< previous page page_209 next page >

Page 209
Listing 10.1 SELCASE.ZIPUsing Select Case to Determine a Student's Letter Grade
Private Sub cmdCalc_Click()
    Dim nSum As Integer
    Dim fAvg As Single
    nSum = Val(txtTest1) + Val(txtTest2) + Val(txtTest3)
    fAvg = nSum / 3
    lblAverage = FormatNumber(fAvg, 1)
    Select Case Round(fAvg)
        Case Is = 100
            lblGrade = "A+"
        Case 93 To 99
            lblGrade = "A"
        Case 83 To 92
            lblGrade = "B"
        Case 73 To 82
            lblGrade = "C"
        Case 63 To 72
            lblGrade = "D"
        Case Is < 63
            lblGrade = "F"
    End Select
End Sub
To test this code, follow these steps:
1. Add three text boxes named txtTest1, txtTest2, and txtTest3 to a form.
2. Add a command button named cmdCalc.
3. As with any form, you should add Label controls as appropriate. In this case, you are using two labels to display the results: lblAverage and lblGrade. Add each label to the form and set the Caption property to an empty string.
4. Enter the code from Listing 10.1 into the Click event procedure of cmdCalc.
5. Click the VB Start button.
Working with Loops
The other major type of control statement is the loop. You use loops to perform repetitive tasks in your program. The three main types of loops supported by Visual Basic are counter loops, conditional loops, and enumerator loops. Counter, or For, loops perform a task a set number of times. Conditional, or Do, loops perform a task while a specified condition exists or until a specified condition exists. Enumerator loops are used to perform an action on each item in a group of objects. Each of these types of loops is discussed in the following sections.

 
< previous page page_209 next page >